<?php
/*错误记录*/
class cpError
{
	//获取ip地址，记录出错信息的时候，记录下ip信息
	static public function getIp()
	{
		if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
		   $ip = getenv("HTTP_CLIENT_IP");
		else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
		   $ip = getenv("HTTP_X_FORWARDED_FOR");
		else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
		   $ip = getenv("REMOTE_ADDR");
		else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
		   $ip = $_SERVER['REMOTE_ADDR'];
		else
		   $ip = "unknown";
		return($ip);
	}
	
	//记录错误信息
	static public function write($message)
	{		
		
		$log_path=cpConfig::get('LOG_PATH');
		//检查日志记录目录是否存在
		 if(!is_dir($log_path)) 
		 {
			//创建日志记录目录
			@mkdir($log_path,0755);
		 }
		 
		 if(substr($log_path, -1) != "/")    
		 {
			$log_path.= "/";
		 }
		 $time=date('Y-m-d H:i:s');
		 $ip=self::getIp();
		 $destination =$log_path .date("Y-m-d").".log";
		 //写入文件，记录错误信息
       	 @error_log("{$time} | {$ip} | {$_SERVER['PHP_SELF']} |{$message}\r\n", 3,$destination);
	}
	//显示错误信息
     static public function show($message)
    {
		 //如果还没有加载配置文件，则加载配置文件
		 if(!defined('is_load_cpConfig'))
		 	require_once(dirname(__FILE__).'/cpConfig.class.php');
		//如果开启了日志记录，则写入日志
		if(cpConfig::get('LOG_ON'))
		{
			self::write($message);
		}
		$error_url=cpConfig::get('ERROR_URL');
		//错误页面重定向
		if($error_url!='')
		{
		 echo '<script language="javascript">
			   if(self!=top)
			  {
				  parent.location.href="'.$error_url.'";
		      }
			  else
			  {
			 	 window.location.href="'.$error_url.'";
			  }
			  </script>';
			exit;
		}
		if(!defined('__APP__'))
		{	
			define('__APP__','/');
		}
		echo 
		'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>系统错误提示!</title>
</head>
<body>
<div style="border:1px solid #9CF; margin:20px auto; width:800px;">
<div style="border:1px solid #fff; padding:15px; background:#f0f6f9;">
<div style="border-bottom:1px #9CC solid; font-size:26px;font-family: "Microsoft Yahei", Verdana, arial, sans-serif; line-height:40px; height:40px; font-weight:bold">系统错误提示!</div>
<div style="height:20px; border-top:1px solid #fff"></div>
<div style="border:1px dotted #F90; border-left:6px solid #F60; padding:15px; background:#FFC">
'.$message.'
</div>
<div style="height:20px;"></div>
<div style=" font-size:15px;">您可以选择 &nbsp;&nbsp;<a href="'.$_SERVER['PHP_SELF'].'" title="重试">重试</a> &nbsp;&nbsp;<a href="javascript:history.back()" title="返回">返回</a>  或者  &nbsp;&nbsp;<a href="'.__APP__.'" title="回到首页">回到首页</a> </div>
</div>
</div>
<div style="font-size:12px; color:#666; text-align:center; line-height:24px; height:24px;"><a href="http://www.yevol.net" title="ems">the E-M-S</a></div>
</body>
</html>';
		exit;
    }
}
?>